// DrawPoint
/*
绘制点
 */
class DrawPoint {
    constructor(args) {
        this.Cesium = args.Cesium;
        this.callback = args.callback;
        this._point = null; //最后一个点
        this._pointData = null; //点数据用于构造点
        this._entities = []; //脏数据
    }
    setViewer(earth) {
        this._viewer = earth.czm.viewer;
    }
    //返回最后活动点
    get point() {
        return this._point;
    }

    //加载点
    loadPoint(data) {
        return this.createPoint(data);
    }

    //返回点数据用于加载点
    getData() {
        return this._pointData;
    }

    //开始绘制
    startCreate() {
        var $this = this;
        this.handler = new this.Cesium.ScreenSpaceEventHandler(
            this._viewer.scene.canvas
        );
        this.handler.setInputAction(function (evt) {
            //单机开始绘制
            var cartesian = $this.getCatesian3FromPX(evt);
            if (!cartesian) return;
            var point = $this.createPoint(cartesian);
            $this._pointData = cartesian;
            $this._point = point;
            if (typeof $this.callback == "function") {
                $this.callback(point);
            }
        }, $this.Cesium.ScreenSpaceEventType.LEFT_CLICK);
    }

    //创建点
    createPoint(cartesian) {
        var $this = this;

        let real_cartesian = cartesian.cartesian2;
        if (
            typeof cartesian.cartesian2 == "undefined" ||
            cartesian.cartesian2.height > 8848 ||
            cartesian.cartesian2.height < 0
        ) {
            real_cartesian = cartesian.cartesian1;
        }

        // 计算经纬度
        var ellipsoid = this._viewer.scene.globe.ellipsoid;
        var cartographic = ellipsoid.cartesianToCartographic(real_cartesian);
        var lat = this.Cesium.Math.toDegrees(cartographic.latitude).toFixed(10);
        var lng = this.Cesium.Math.toDegrees(cartographic.longitude).toFixed(10);
        var height = cartographic.height.toFixed(3);
        console.log(lng + ',' + lat)

        let guid = this.generateUUID();
        this._viewer.entities.add({
            id: guid,
            position: real_cartesian,
            point: {
                pixelSize: 10,
                color: $this.Cesium.Color.YELLOW
            },
            label: {
                text: "纬度:" + lat + "\n经度:" + lng + "\n高程:" + height,
                font: "24px Helvetica",
                fillColor: this.Cesium.Color.SKYBLUE,
                outlineColor: this.Cesium.Color.BLACK,
                outlineWidth: 2,
                style: this.Cesium.LabelStyle.FILL_AND_OUTLINE,
                pixelOffset: new this.Cesium.Cartesian2(0, -75)
            }
        });
        $this._entities.push(guid); //加载脏数据
        return guid;
    }

    //销毁鼠标事件
    destroy() {
        if (this.handler) {
            this.handler.destroy();
            this.handler = null;
        }
    }

    //清空实体对象
    clear() {
        for (var i = 0; i < this._entities.length; i++) {
            this._viewer.entities.removeById(this._entities[i]);
        }
        this._entities = [];
        this._point = null;
    }

    getCatesian3FromPX(px) {
        // 两个pick联合使用，可以解决pickPosition不稳定的问题和global.pick不能找模型高度的问题
        var cartesian1;
        var cartesian2;
        var ray = this._viewer.camera.getPickRay(px.position);
        if (!ray) return null;
        cartesian1 = this._viewer.scene.globe.pick(ray, this._viewer.scene);

        if (this._viewer.scene.pickPositionSupported) {
            if (this._viewer.scene.mode !== Cesium.SceneMode.MORPHING) {
                this._viewer.scene.globe.depthTestAgainstTerrain = true;
                cartesian2 = this._viewer.scene.pickPosition(px.position);
            }
        } else {
            //如果不支持pickPosition,则返回undefined
            console.log("This browser does not support pickPosition.");
            cartesian2 = undefined;
        }
        return { cartesian1: cartesian1, cartesian2: cartesian2 };
    }

    /**
     * generateUUID 生成UUID
     * @returns {string} 返回字符串
     */
    generateUUID() {
        var d = new Date().getTime();
        var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (
            c
        ) {
            var r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            return (c == "x" ? r : (r & 0x7) | 0x8).toString(16);
        });
        return uuid;
    }
}

export default DrawPoint;